package ma.ensao.egicom.core.services.implementations;

import java.util.ArrayList;

import ma.ensao.egicom.core.dao.interfaces.ProductDAO;
import ma.ensao.egicom.core.dto.CaddieLineDto;
import ma.ensao.egicom.core.dto.ProductDto;
import ma.ensao.egicom.core.model.Product;
import ma.ensao.egicom.core.services.interfaces.CaddieService;

public class CaddieServiceImpl implements CaddieService {

    private ProductDAO productDao;

   // private MapperDozerBean mapperDozerBean;

    float sum;

    @Override
    public float calculatePricesSum(ArrayList<CaddieLineDto> productList) {
        sum = 0.0f;
        for (CaddieLineDto cdto : productList) {

            ProductDto pro = cdto.getProduct();
            sum += pro.getPrice() * (cdto.getQuantity());
        }
        return sum;
    }

    @Override
    public boolean verificateDispoProduct(Long idProduit) {

        Product productBean = productDao.findById(idProduit);
        if (productBean == null) {
            return false;
        }
        return true;
    }

    @Override
    public ArrayList<CaddieLineDto> removeProduct(ArrayList<CaddieLineDto> productList, Long productId) {

        for (CaddieLineDto cdto : productList) {

            ProductDto pro = cdto.getProduct();
            if (new Long(pro.getId()) == productId) {
                cdto.setQuantity(cdto.getQuantity() - 1);
            }

        }
        return productList;

    }

    @Override
    public ArrayList<CaddieLineDto> addProduct(ArrayList<CaddieLineDto> productList, Long productId) {
        int cmp = 0;
        for (CaddieLineDto cdto : productList) {

            ProductDto pro = cdto.getProduct();
            if (Long.valueOf(pro.getId().longValue()).equals(productId)) {
                cdto.setQuantity(cdto.getQuantity() + 1);
                cmp++;
            }
        }
        if (cmp == 0) {
            CaddieLineDto cadto = new CaddieLineDto();
            ProductDto prodto = new ProductDto();
            Product productBean = productDao.findById(productId);
            prodto.setId((int) (long) productId);
            prodto.setPrice(productBean.getPrice());
            cadto.setProduct(prodto);
            cadto.setQuantity(+1);
            productList.add(cadto);
        }
        return productList;

    }

    public ProductDAO getProductDao() {
        return productDao;
    }

    public void setProductDao(ProductDAO productDao) {
        this.productDao = productDao;
    }

}
